<?php
class Account extends Model
{
	/**
	 * 
	 * Enter description here ...
	 * @var Person
	 */
	private $person;
	
	/**
	 * 
	 * Enter description here ...
	 * @var boolean
	 */
	private static $loggedIn;

	/**
	 * 
	 * Enter description here ...
	 * @return Person
	 */
	public function getPerson()
	{
		if(!isset($this->person) && $this->person_nefub_id)
		{
			$this->person = Person::getByNefubId($this->person_nefub_id);
		}
		return $this->person;
	}
	
	
	/**
	 * Log in to an admin account
	 *
	 * @param string $username
	 * @param string $password
	 * @return bool
	 */
	public static function login($username, $password)
	{
		$wheres = array
		(
			'username' => $username,
			'password' => md5($password)
		);
		
		$rows = self::getAll($wheres);
		if(count($rows) > 0)
		{
			$account = $rows[0];		
			$_SESSION[LOGIN] = $account;
			self::$loggedIn = $account;
			return true;
		}		
		return false;		
	}
	
	/**
	 * Remove Account from the session
	 * @return void
	 */
	public static function logout()
	{
		self::$loggedIn = null;
		$_SESSION[LOGIN] = null;
		unset($_SESSION[LOGIN]);
	}
	
	/**
	 * Enter description here...
	 * @return Account
	 */
	public static function getLoggedIn()
	{
		// retrieved before in current session
		if(self::$loggedIn)
		{
			return self::$loggedIn;
		}
		
		// was logged in, but not retrieved before
		if($_SESSION[LOGIN] instanceof Account)
		{
			self::$loggedIn = $_SESSION[LOGIN];
			return self::$loggedIn;
		}
		
		// not logged in
		return false;
	}
}